// 存放所有路由的地方

const express = require('express')
const router = express.Router()
const userHandler = require('../router_handler/user')
const bodyParser = require('body-parser');

// 导入验证表单数据的中间件
const expressJoi = require('@escook/express-joi')
// 导入验证规则对象
const { reg_login_schema } = require('../schema/user')

router.use(bodyParser.json());
// 配置跨域
router.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Headers', '*');
    res.setHeader('Access-Control-Allow-Methods', '*');
    next();
})

// 响应数据的中间件
router.use((req, res, next) => {
    res.cc = (err, status = 1) => {
        res.send({
            status,
            message: err instanceof Error ? err.message : err
        })
    }
    next()
})

// 注册新用户
router.post('/register', expressJoi(reg_login_schema),userHandler.regUser)

// 登录
router.post('/login', userHandler.login)

module.exports = router